import { deletevideos, textureinfo } from "@/api/virtualhuman";
import { Message } from 'element-ui';
import { pageIndexs, jpgScreen, disposeNow } from "@/utils/SaveFunction";
const state = {
    userProjectInfo: {
        userid: 0,//用户id
        distinguishability: 1,//1是16：9，2是9：16
        uuid: 0,//项目id已确认
        text: "",//总文字已确认
        name: "",//项目名字
        bgMusic: "", // 背景音乐
        path: "",//目标生成地址
        level: 1,//视频质量 自动 - 1 流畅 -2 清晰 -3   高清 -4
        subtitle: 2,//是否开启字幕 1是 2否
        character: {
            uuid: 0,//人物id已确认
            icon: "", //人物头像已确认
            video: "", //人物视频已确认
            staticImg: "", // 人物图片已确认
            name: "", //人物名字已确认
            type: 1,//人物type已确认
            voiceName: "", //name_show 音色已经确认
            tonename: "",//音色名回显用
            voiceId: 0,//语音id
            width: 225,//人物宽度已确认
            height: 300,//人物高度已确认
            X: 333, //人物坐标已经确认
            Y: 261, //人物top坐标确认
        },
        ppt: { //ppt
            istts: 2, //是否为全局录音 1是 ， 2否
            maxNum: 1, // ppt多少页已确认
            name: "", // ppt名字已确认
            tts: "",//音频已确认
            uuid: 0,//pptId已确认
            pages: [
                {
                    icon: "",//没意义
                    img: "",//图片已确认
                    switchType: "",//没意义
                    text: "", //当页的文字已确认
                    textdome: "",
                    dome: [],
                    tts: "", //音频已确认
                    enlargeStartTime: 0, // 放大开始时间（暂时不做）
                    enlargeEndTime: 0, //放大结束时间（暂时不做）
                    video: "",//插入的视频
                    videoid: 0,//插入的视频id
                    isSound: 1,//是否开启声音1是2否
                    isVideo: 2, // 是否有视频1是2否
                    index: 0,//数组下标
                    pageMouthVideo: "",//当页人嘴视频
                    childList: [
                        {
                            ttsText: "请输入第",//分页的文字
                            tts: "", //分页文字组成的语音
                            mouthVideo: "", //嘴型视频
                            isScale: 2  // 是否放大
                        }
                    ],
                    textureList: [
                        // {
                        //     position :10, // int  `json:"position" comment:"相对当前文字的位置"` 
                        //     width    :10, // int  `json:"width" comment:"贴图宽度"`
                        //     height   :10, // int  `json:"height" comment:"贴图高度"`
                        //     x        :10, // int  `json:"X" comment:"贴图x坐标"`
                        //     y        :10, // int  `json:"Y" comment:"贴图y坐标"`
                        //     textureId:10 //  int `json:"textrueid" comment:"贴图id"` 
                        // } 
                    ],
                }
            ]
        },
        scene: {
            icon: "", //背景图已确认
            name: "",//背景图名字已确认
            // backWidth: 960, //背景图片的宽已确认
            // backHeight: 540,//背景图片的高已确认
            type: 1,//背景图片类型
            pptWidth: 294,//电视的高已确认
            pptHeight: 168,//电视的高已确认
            pptX: 586, //电视的位置已确认
            pptY: 162,//电视的位置已确认
        }
    }
}
const getters = {
    pagesuuids(state) {//取项目id
        return state.userProjectInfo.uuid
        // return 1
    },
    getdistinguishability(state) {//取比例
        return state.userProjectInfo.distinguishability
    },
    getjsonname(state) { //取ppt名字
        return state.userProjectInfo.name
    },
    gettonename(state) { //取人物音色
        return state.userProjectInfo.character.tonename
    },
    Xna(state) {//取人物X坐标
        return state.userProjectInfo.character.X
    },
    Yna(state) {//取人物Y坐标
        return state.userProjectInfo.character.Y
    },
    users(state) {//取人物信息，好设置图片
        return state.userProjectInfo.character
    },
    userwitdh(state) {//取人物宽
        return state.userProjectInfo.character.width
    },
    userheight(state) {//取人物高
        return state.userProjectInfo.character.height
    },
    pptx(state) {//ppt坐标
        return state.userProjectInfo.scene.pptX
    },
    ppty(state) {//ppt坐标
        return state.userProjectInfo.scene.pptY
    },
    pptwidth(state) {//ppt宽
        return state.userProjectInfo.scene.pptWidth
    },
    pptheight(state) {//ppt高
        return state.userProjectInfo.scene.pptHeight
    },
    pptindex(state) {//获取当前的ppt列表里面有文字和ppt列表
        // 在选择PPTTUBView.vue里有一个num，所以就可以一起返回回来了
        return state.userProjectInfo.ppt.pages
    },
    pptid(state) { //取pptid
        return state.userProjectInfo.ppt.uuid
    },
    ppttext(state) {
        // 在选择PPTTUBView.vue里有一个num，所以就可以一起返回回来了
        //因为他需要修改文字，但是又不可以修改vuex所以取消一下双向绑定
        return state.userProjectInfo.ppt.pages
    },
    json(state) { //获取ppt总表
        return state.userProjectInfo
    },
    backgr(state) { //背景图
        return state.userProjectInfo.scene.icon
    },
    backwid(state) { //背景图片的宽度
        return state.userProjectInfo.scene.backWidth
    },
    backheig(state) { // 背景图片的高度
        return state.userProjectInfo.scene.backHeight
    },
    textvideouser(state) { //取音色好合成
        return state.userProjectInfo.character.voiceName
    },
    isttss(state) { //是否有音频
        return function (index) {
            return state.userProjectInfo.ppt.pages[index].istts
        }
    },
    getTextureList(state) {
        return function (index) {
            return state.userProjectInfo.ppt.pages[index].textureList
        }
    },
    getlevel(state) { //取视频质量
        return state.userProjectInfo.level
    },
    getsubtitle(state) { //取字幕
        return state.userProjectInfo.subtitle
    }
}
const mutations = {
    REMOVEJSON(state) {
        //初始化json
        state.userProjectInfo = {
            userid: 0,//用户id
            distinguishability: 1,//1是16：9，2是9：16
            uuid: 0,//项目id已确认
            text: "",//总文字已确认
            name: "",//项目名字
            bgMusic: "", // 背景音乐
            path: "",//目标生成地址
            level: 1,//视频质量 自动 - 1   流畅 -2   清晰 -3     高清 -4
            subtitle: 2,//是否开启字幕 1是 2否
            character: {
                uuid: "",//人物id已确认
                icon: "", //人物头像已确认
                video: "", //人物视频已确认
                staticImg: "", // 人物图片已确认
                name: "", //人物名字已确认
                type: 1,//人物type已确认
                voiceName: "", //name_show 音色已经确认
                tonename: "",//音色名回显用
                width: 225,//人物宽度已确认
                height: 300,//人物高度已确认
                X: 333, //人物坐标已经确认
                Y: 261, //人物top坐标确认
                lipsVideo: "" // 嘴型的视频在2变3时插入(不用后端用)
            },
            ppt: { //ppt
                istts: 1, //是否为全局录音 1是 ， 2否
                maxNum: 1, // ppt多少页已确认
                name: "", // ppt名字已确认
                tts: "",//音频已确认
                uuid: 0,//pptId已确认
                pages: [
                    {
                        duration: 1000,//没意义
                        icon: "",//没意义
                        img: "",//图片已确认
                        switchType: "",//没意义
                        text: "", //当页的文字已确认
                        textdome: "",
                        dome: [],
                        tts: "", //音频已确认
                        enlargeStartTime: 0, // 放大开始时间（暂时不做）
                        enlargeEndTime: 0, //放大结束时间（暂时不做）
                        video: "",//插入的视频
                        videoid: 0,//插入的视频id
                        isSound: 1,//是否开启声音1是2否
                        isVideo: 2, // 是否有视频1是2否
                        index: 0,//数组下标
                        childList: [],
                        pageMouthVideo: ""//当页人嘴视频
                    }
                ]
            },
            scene: {
                icon: "", //背景图已确认
                name: "",//背景图名字已确认
                // backWidth: 960, //背景图片的宽已确认
                // backHeight: 540,//背景图片的高已确认
                type: 1, // 背景图类型已确认1. 1920*1080 2. 1080*1920
                pptWidth: 294,//电视的高已确认
                pptHeight: 168,//电视的高已确认
                pptX: 586, //电视的位置已确认
                pptY: 162,//电视的位置已确认
            }
        }
    },
    ISTTS(state, index) {

    },
    SETCLIENTSIDE(state) {//设置16：9场景
        state.userProjectInfo.distinguishability = 1
    },
    CLIENTSIDESET(state) {//设置9：16场景
        state.userProjectInfo.distinguishability = 2
    },
    SETJSONNAME(state, name) {
        //设置json名字
        state.userProjectInfo.name = name
    },
    //空PPT判断
    PPTTEXTEMPTY(state) {
        state.userProjectInfo.ppt.uuid = 0
        state.userProjectInfo.ppt.maxNum = 1
        state.userProjectInfo.ppt.name = ""
        state.userProjectInfo.ppt.pages = []
        //不这样判断合成会报错，无法检测到是否传了语音
        for (let index = 0; index < 1; index++) {
            state.userProjectInfo.ppt.pages.push({
                istts: 2,
                duration: 1000,//没意义
                icon: "",//没意义
                img: "",
                switchType: "",//没意义
                text: ``, //当页的文字
                textdome: `请输入第${index + 1}页的内容，输入后请点击保存`,
                dome: [],
                tts: "", //音频
                enlargeStartTime: 0, // 放大开始时间（暂时不做）
                enlargeEndTime: 0, //放大结束时间（暂时不做）
                video: "",
                videoid: 0,//视频id
                isSound: 1,//是否开启声音1是2否
                isVideo: 2, // 是否有视频1是2否
                isScale: 2,//1是否全屏是2否
                index: index,//合成顺序下标
                childList: [],
                pageMouthVideo: ""//当页人嘴视频
            })
        }
    },
    SETLEVEL(state, num) { //设置视频质量
        state.userProjectInfo.level = num
    },
    SETSUBTITLE(state, num) {
        state.userProjectInfo.subtitle = num
    },
    BAKWID(state, widheg) { //设置背景图片宽高度
        state.userProjectInfo.scene.backWidth = widheg.wid
        state.userProjectInfo.scene.backHeight = widheg.heg
    },
    TIMBER(state, { name, tonename, voiceId }) { //设置人物音色
        state.userProjectInfo.character.voiceName = name
        state.userProjectInfo.character.tonename = tonename
        state.userProjectInfo.character.voiceId = voiceId
    },
    COORDINATE(state, { x, y, width, height }) {//人物调整大小
        state.userProjectInfo.character.X = x
        state.userProjectInfo.character.Y = y
        state.userProjectInfo.character.width = width
        state.userProjectInfo.character.height = height
    },
    DRAGUSER(state, { x, y }) {//人物移动
        state.userProjectInfo.character.X = x
        state.userProjectInfo.character.Y = y
    },
    USERIMAGES(state, user) { //人物相关
        state.userProjectInfo.character.uuid = user.id
        state.userProjectInfo.character.icon = user.cover//人物头像
        state.userProjectInfo.character.name = user.name //人物名字
        state.userProjectInfo.character.staticImg = user.static_img//人物图片
        state.userProjectInfo.character.video = user.video//人物s视频
        state.userProjectInfo.character.type = user.type//人物类型
        // state.userProjectInfo.character.voiceName = user.timbreInfo.name //音色
    },
    PPTNUMBER(state, pptuser) {//设置ppt
        state.userProjectInfo.ppt.pages[pptuser.item].img = pptuser.val
    },
    BACKGROUNDS(state, bgd) { //背景图片
        state.userProjectInfo.scene.icon = bgd.path //背景图片路径
        state.userProjectInfo.scene.name = bgd.name//背景图片名字
        state.userProjectInfo.scene.type = bgd.type//背景图片类型
    },
    PPTMAX(state, max) { //当用户选择ppt的时候就设有多少页
        state.userProjectInfo.ppt.uuid = max.id
        state.userProjectInfo.ppt.maxNum = max.pageNum
        state.userProjectInfo.ppt.name = max.name
        state.userProjectInfo.ppt.pages = []
        for (let index = 0; index < max.pageNum; index++) {
            state.userProjectInfo.ppt.pages.push({
                istts: 2,
                duration: 1000,//没意义
                icon: "",//没意义
                img: "",
                switchType: "",//没意义
                text: `请输入第${index + 1}页的内容，输入后请点击保存`, //当页的文字
                textdome: `请输入第${index + 1}页的内容，输入后请点击保存`,
                dome: [],
                tts: "", //音频
                enlargeStartTime: 0, // 放大开始时间（暂时不做）
                enlargeEndTime: 0, //放大结束时间（暂时不做）
                video: "",
                videoid: 0,//视频id
                isSound: 1,//是否开启声音1是2否
                isVideo: 2, // 是否有视频1是2否
                isScale: 2,//1是否全屏是2否
                index: index,//合成顺序下标
                childList: [],
                textureList: [
                    // {
                    //     position :10, // int  `json:"position" comment:"相对当前文字的位置"` 
                    //     width    :10, // int  `json:"width" comment:"贴图宽度"`
                    //     height   :10, // int  `json:"height" comment:"贴图高度"`
                    //     x        :10, // int  `json:"X" comment:"贴图x坐标"`
                    //     y        :10, // int  `json:"Y" comment:"贴图y坐标"`
                    //     TextrueId:10 //  int `json:"textrueid" comment:"贴图id"` 
                    // } 
                ],
                pageMouthVideo: ""//当页人嘴视频
            })
        }
        for (let indexs = 0; indexs < max.pageNum; indexs++) {
            state.userProjectInfo.ppt.pages[indexs].img = max.basePath + max.uuidStr + `/images/${indexs + 1}.png`//图片已确认
            // max.basePath + max.uuidStr + `/images/${index}.png`,//图片已确认
        }
    },
    PPTLISTADD(state) { //点击添加场景
        state.userProjectInfo.ppt.pages.push({
            duration: 1000,//没意义
            icon: "",//没意义
            img: "",//图片已确认
            switchType: "",//没意义
            text: "", //当页的文字
            textdome: "",
            dome: [],
            tts: "", //音频
            enlargeStartTime: 0, // 放大开始时间（暂时不做）
            enlargeEndTime: 0, //放大结束时间（暂时不做）
            video: "",
            videoid: 0,
            index: state.userProjectInfo.ppt.pages.length,
            childList: [],
            textureList: []
        })
        state.userProjectInfo.ppt.maxNum = state.userProjectInfo.ppt.pages.length
    },

    JSONPRIONFO(state, userProjectInfo) { //用户详情回显
        state.userProjectInfo = userProjectInfo
    },
    JSONPRIONFOUUID(state, uuid) { //设置项目id
        state.userProjectInfo.uuid = uuid
    },
    SETPPTWIDTH(state, pptindex) {
        //设置调整ppt的宽高大小
        state.userProjectInfo.scene.pptWidth = pptindex.pptWidth
        state.userProjectInfo.scene.pptHeight = pptindex.pptHeight
        state.userProjectInfo.scene.pptX = pptindex.pptX
        state.userProjectInfo.scene.pptY = pptindex.pptY
    },
    SETHEIGHT(state, pptindex) {
        //设置移动ppt的位置
        state.userProjectInfo.scene.pptX = pptindex.pptX
        state.userProjectInfo.scene.pptY = pptindex.pptY
    },
    PAGEADD(state, pptindex) {
        //复制
        state.userProjectInfo.ppt.pages.splice(pptindex + 1, 0, JSON.parse(JSON.stringify(state.userProjectInfo.ppt.pages[pptindex])))
        state.userProjectInfo.ppt.pages[pptindex + 1].textdome += "复制页"
        state.userProjectInfo.ppt.maxNum = state.userProjectInfo.ppt.pages.length
        for (let index = 0; index < state.userProjectInfo.ppt.maxNum; index++) {
            state.userProjectInfo.ppt.pages[index].index = index
        }
    },
    PAGEDEL(state, pptindex) {
        //删除
        state.userProjectInfo.ppt.pages.splice(pptindex, 1)
        state.userProjectInfo.ppt.maxNum = state.userProjectInfo.ppt.pages.length
        for (let index = 0; index < state.userProjectInfo.ppt.maxNum; index++) {
            state.userProjectInfo.ppt.pages[index].index = index
        }
    },
    ISALTERTTS(state, index) {//修改局部的音频状态
        let item = state.userProjectInfo.ppt.pages[index]
        item.text = ""
        item.textdome = ""
        item.textureList = []
        item.childList = []
        item.dome = []
        item.istts = 1

    },
    PPTTTSADD(state, ttsobj) {
        //上传音频
        state.userProjectInfo.ppt.pages[ttsobj.index].tts = ttsobj.tts
    },
    REMOVETTS(state, index) {
        //恢复成文本模式
        state.userProjectInfo.ppt.pages[index].istts = 2
        state.userProjectInfo.ppt.pages[index].tts = ""
    },
    PPTPAGASNUM(state, obj) { //把上传的语音放上去
        state.userProjectInfo.ppt.pages[obj.nums].tts = obj.url
    },
    PPTPAGEVIDEO(state, obj) { //把上传的视频插入进去
        state.userProjectInfo.ppt.pages[obj.nums].video = obj.video
        state.userProjectInfo.ppt.pages[obj.nums].videoid = obj.videoid
        state.userProjectInfo.ppt.pages[obj.nums].isVideo = 1
    },
    VIDEOMUTE(state, EXTindex) { //关闭视频
        state.userProjectInfo.ppt.pages[index].isSound = 2
    },
    TURNONTHESOUND(state, index) {   //开启视频声音
        state.userProjectInfo.ppt.pages[index].isSound = 1
    },
    PPTVIDEODEL(state, index) { //删除当前页视频
        state.userProjectInfo.ppt.pages[index].video = ""
        state.userProjectInfo.ppt.pages[index].videoid = 0
        state.userProjectInfo.ppt.pages[index].isVideo = 0
    },
    PPTTEXT(state, textNum) { //输入的文字
        state.userProjectInfo.ppt.pages[textNum.num].text = textNum.text
        state.userProjectInfo.ppt.pages[textNum.num].textdome = textNum.HTMLText
        state.userProjectInfo.ppt.pages[textNum.num].dome = textNum.screen
        state.userProjectInfo.ppt.pages[textNum.num].gifdome = textNum.jpg
        state.userProjectInfo.ppt.pages[textNum.num].childList = []
    },
    PPTTEXTDIVIDE(state, indexs) {//处理放大缩小
        let obj = state.userProjectInfo.ppt.pages[indexs]
        //处理全屏
        obj.childList = pageIndexs(obj)
        //处理好的textureList
        let futureChildList = jpgScreen(obj.textdome, obj.gifdome, obj.dome)
        let nowChildList = obj.textureList
        //处理刷新之后会重置图片的问题
        obj.textureList = disposeNow(futureChildList || [], nowChildList)
    },
    //保存page
    PPTFUGAI(state, { page, index }) {
        state.userProjectInfo.ppt.pages[index].textureList = page.textureList
        state.userProjectInfo.ppt.pages[index] = page
        Message.success("保存成功")
    },
    //调整jpg（贴图）
    DRAJPG(state, { num, index, left, top, width, height }) {
        let jpg = state.userProjectInfo.ppt.pages[num].textureList[index]
        jpg.height = height
        jpg.x = left
        jpg.y = top
        jpg.width = width
    },
    //挪动jpg
    RESIZJPG(state, { num, index, left, top }) {
        let jpg = state.userProjectInfo.ppt.pages[num].textureList[index]
        jpg.x = left
        jpg.y = top
    },
}
const actions = {
    ppptvidelaxios(mutations, objs) {
        return new Promise((ress, rej) => {
            var obj = {
                path: objs.path,
                resid: objs.id,
                projectid: objs.uuid - 0
            }
            deletevideos(obj).then(res => {
                if (res.code == 200) {
                    mutations.commit("PPTVIDEODEL", objs.index)
                    ress(res.msg)
                } else {
                    rej(res.msg)
                }
            })
        })
    },
    //处理imgpath路径问题
    pptindeximg(mutations, { imgarr, indexs }) {
        return new Promise(async (ress, rej) => {
            let textureList = imgarr.textureList
            if (textureList !== undefined && textureList.length > 0) {
                let index = textureList.length
                textureList.forEach((item) => {
                    textureinfo({
                        textureId: item.textureId
                    }).then(res => {
                        if (res.code === 200) {
                            index--
                            item.path = res.data.path
                            if (index === 0) {
                                mutations.commit("PPTFUGAI", { page: imgarr, index: indexs })
                                ress()
                            }
                        } else {
                            Message.error(res.msg)
                        }
                    })
                })
            } else {
                mutations.commit("PPTFUGAI", { page: imgarr, index: indexs })
                ress()
            }
        })
    }
}
export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}